﻿public partial class Solution
{
    /// <summary>
    /// 有效的括号
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public bool IsValid(string s)
    {
        bool result = false;
        Dictionary<char, char> kv = new Dictionary<char, char>
        {
            { ')', '(' },
            { ']', '[' },
            { '}', '{' },
        };
        if (s.Length % 2 == 0)
        {
            Stack<char> st = new Stack<char>();
            bool loop = true;
            foreach (char ch in s)
            {
                if (ch is '(' or '[' or '{')
                {
                    st.Push(ch);
                }
                else
                {
                    if (st.Count > 0 && kv[ch] == st.Peek())
                    {
                        st.Pop();
                    }
                    else
                    {
                        loop = false;
                        break;
                    }
                }
            }
            if (loop)
            {
                result = st.Count == 0;
            }
        }
        return result;
    }
}